import { _decorator, Button, Label, UITransform } from 'cc';
import { BasePopUp } from '../../../frame/component/ui.pop-up';
import GameDC from '../../data/data.game.center';
import ConfManager from '../../manager/config.manager';
import DataManager from '../../manager/data.manager';
import type { Popup } from '../page/pop-up';
const { ccclass, property } = _decorator;

@ccclass('ppGiftBlocker')
export class ppGiftBlocker extends BasePopUp<typeof Popup, null> {
	protected default_return: null;

	private ui_gift: UITransform;
	private ui_blocker: UITransform;

	protected initAfterOnLoad(){
		this.init();
		this.initUIStatus();
	}

	public onShow(){
	}

	private init(){
		this.ui_gift = this.node.getChildByName('gift').getComponent(UITransform);
		this.ui_blocker = this.node.getChildByName('blocker').getComponent(UITransform);
	}

	private initUIStatus(){
		DataManager.getGoodsInfo(ConfManager.otherConf.buffPrice.superGiftGoodsId).then(info=>this.ui_gift.node.getChildByName('lbl-reap').getComponent(Label).string = info.localPrice);
		DataManager.getGoodsInfo(ConfManager.otherConf.buffPrice.removeAdPackGoodsId).then(info=>this.ui_blocker.node.getChildByName('lbl-reap').getComponent(Label).string = info.localPrice);

		this.ui_blocker.node.getChildByName('lbl-head').getComponent(Label).string = 'Remove ADs Pack';
		this.ui_blocker.node.getChildByName('restore').getComponent(Label).string = 'Restore';
		this.ui_blocker.node.getChildByName('lbl-diamond').getComponent(Label).string = 'x350';
	}

	protected _click_event_: { [name: string]: (this: ppGiftBlocker, button: Button) => void; } = {
		close(btn){
			this.close(null);
		},
		'reap-gift'(btn){
			btn.interactable = false;
			this.pop.Wait(DataManager.buyGBGoods('gift'))
			.then(()=>{
				let success = !GameDC.User.gift;
				btn.interactable = !success;
				if(success){
					this.ui_gift.node.active = false;
					if(this.ui_blocker.node.active){
						return void 0;
					}else{
						this.close(null);
					}
				}
			});
		},
		'reap-blocker'(btn){
			btn.interactable = false;
			this.pop.Wait(DataManager.buyGBGoods('blocker'))
			.then(()=>{
				let success = !GameDC.User.adblock;
				btn.interactable = !success;
				if(success){
					this.ui_blocker.node.active = false;
					DataManager.changeDiamond(350);
					DataManager.floatDiamond(350, mtec.cc.canvas.node.position);
					if(this.ui_gift.node.active){
						return void 0;
					}else{
						this.close(null);
					}
				}
			});
		}
	}
}

